package com.huawei.hms.framework.network.grs.local;

import android.text.TextUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.grs.GrsBaseInfo;
import com.huawei.hms.framework.network.grs.local.model.b;
import com.huawei.hms.framework.network.grs.local.model.c;
import com.huawei.hms.framework.network.grs.local.model.d;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

public abstract class a {
    com.huawei.hms.framework.network.grs.local.model.a a;
    List<b> b;
    Map<String, String> c = new ConcurrentHashMap(16);
    boolean d = false;
    Map<String, String> e = new ConcurrentHashMap(16);

    private Map<String, String> a(List<b> list, GrsBaseInfo grsBaseInfo) {
        Map<String, String> concurrentHashMap = new ConcurrentHashMap(16);
        concurrentHashMap.put("no_route_country", "no-country");
        for (b bVar : list) {
            if (bVar.b().contains(grsBaseInfo.getIssueCountry())) {
                concurrentHashMap.put(grsBaseInfo.getIssueCountry(), bVar.a());
            }
            if (bVar.b().contains(grsBaseInfo.getRegCountry())) {
                concurrentHashMap.put(grsBaseInfo.getRegCountry(), bVar.a());
            }
            if (bVar.b().contains(grsBaseInfo.getSerCountry())) {
                concurrentHashMap.put(grsBaseInfo.getSerCountry(), bVar.a());
            }
        }
        return concurrentHashMap;
    }

    private int e(String str) {
        if (TextUtils.isEmpty(str)) {
            Logger.w("AbstractLocalManager", "getConfigMgr configContent is null.");
            return -1;
        }
        int c = c(str);
        if (c != 0) {
            return c;
        }
        c = a(str);
        return c == 0 ? b(str) : c;
    }

    private int f(String str) {
        if (TextUtils.isEmpty(str)) {
            Logger.w("AbstractLocalManager", "getConfigMgr configContent is null.");
            return -1;
        }
        if (this.b == null || this.b.isEmpty()) {
            int c = c(str);
            if (c != 0) {
                return c;
            }
        }
        return b(str);
    }

    public abstract int a(String str);

    public String a(GrsBaseInfo grsBaseInfo, String str, String str2) {
        Map a = a(grsBaseInfo, str);
        if (a != null) {
            return (String) a.get(str2);
        }
        Logger.w("AbstractLocalManager", "addresses not found by routeby in local config{%s}", str);
        return null;
    }

    public Map<String, String> a(GrsBaseInfo grsBaseInfo, String str) {
        if (!this.d) {
            return null;
        }
        c b = this.a.b(str);
        if (b == null) {
            Logger.w("AbstractLocalManager", "service not found in local config{%s}", b);
            return null;
        }
        String a = d.a(b.a(), grsBaseInfo);
        if (a == null) {
            Logger.w("AbstractLocalManager", "country not found by routeby in local config{%s}", b.a());
            return null;
        }
        List b2 = b.b();
        d b3 = (b2 == null || b2.size() == 0) ? b.b((String) this.c.get(a)) : b.b((String) a(b2, grsBaseInfo).get(a));
        return b3 == null ? null : b3.b();
    }

    public boolean a(GrsBaseInfo grsBaseInfo) {
        if (this.b == null) {
            Logger.w("AbstractLocalManager", "updateCountryGroupMap return null because {null == countryGroups}");
            return false;
        }
        this.c.put("no_route_country", "no-country");
        for (b bVar : this.b) {
            if (bVar.b().contains(grsBaseInfo.getIssueCountry())) {
                this.c.put(grsBaseInfo.getIssueCountry(), bVar.a());
            }
            if (bVar.b().contains(grsBaseInfo.getRegCountry())) {
                this.c.put(grsBaseInfo.getRegCountry(), bVar.a());
            }
            if (bVar.b().contains(grsBaseInfo.getSerCountry())) {
                this.c.put(grsBaseInfo.getSerCountry(), bVar.a());
            }
        }
        return true;
    }

    public abstract int b(String str);

    public abstract int c(String str);

    int d(String str) {
        int i = -1;
        if (e(com.huawei.hms.framework.network.grs.c.d.a(str)) == 0) {
            Logger.i("AbstractLocalManager", "load APP_CONFIG_FILE success.");
            i = 0;
        }
        try {
            String[] list = com.huawei.hms.framework.network.grs.c.b.a().getAssets().list("");
            if (list != null && list.length > 0) {
                for (String str2 : list) {
                    if (Pattern.matches("^grs_sdk_global_route_config_[a-zA-Z]+\\.json$", str2) && f(com.huawei.hms.framework.network.grs.c.d.a(str2)) == 0) {
                        Logger.i("AbstractLocalManager", "load SDK_CONFIG_FILE sucess.");
                        i = 0;
                    }
                }
            }
        } catch (IOException e) {
            Logger.w("AbstractLocalManager", "list assets files fail,please check if according to our standard config json files.");
        }
        return i;
    }
}
